// Copyright 2025 Laszlo Consulting Kft.
// SPDX-License-Identifier: Apache-2.0

import { JSX } from "solid-js";
import type { DaemonSet, DaemonSetWithResources } from "../../types/k8s.ts";
import { Filter } from "../filterBar/FilterBar.tsx";
import { useCalculateAge } from "./timeUtils.ts";
import { sortByName, sortByAge } from '../../utils/sortUtils.ts';

// Helper function to determine readiness status
function getReadinessComponent(daemonSet: DaemonSet): { element: JSX.Element, title: string } {
  const desiredScheduled = daemonSet?.status?.desiredNumberScheduled;
  const currentScheduled = daemonSet?.status?.currentNumberScheduled;
  const numberReady = daemonSet?.status?.numberReady;
  const numberAvailable = daemonSet?.status?.numberAvailable || 0;
  const numberUnavailable = daemonSet?.status?.numberUnavailable || 0;
  
  const isReady = numberReady === desiredScheduled && numberUnavailable === 0;
  const statusClass = isReady ? "text-success" : "text-warning";
  
  return {
    element: (
      <span class={statusClass}>
        {numberReady}/{desiredScheduled}
      </span>
    ),
    title: `Ready: ${numberReady}/${desiredScheduled}, Available: ${numberAvailable}, Unavailable: ${numberUnavailable}`
  };
}

const getPodColor = (status: string) => {
  switch (status) {
    case "Running":
      return "var(--linear-green)";
    case "Pending":
      return "var(--linear-yellow)";
    case "Failed":
      return "var(--linear-red)";
    default:
      return "var(--linear-gray)";
  }
};

// Define the columns for the DaemonSet resource list
export const daemonSetColumns = [
  {
    header: "NAME",
    width: "25%",
    accessor: (daemonSet: DaemonSet) => <>{daemonSet.metadata.name}</>,
    title: (daemonSet: DaemonSet) => daemonSet.metadata.name,
    sortable: true,
    sortFunction: (items: any[], ascending: boolean) => sortByName(items, ascending),
  },
  {
    header: "DESIRED",
    width: "10%",
    accessor: (daemonSet: DaemonSet) => <>{daemonSet?.status?.desiredNumberScheduled}</>,
  },
  {
    header: "CURRENT",
    width: "10%",
    accessor: (daemonSet: DaemonSet) => <>{daemonSet?.status?.currentNumberScheduled}</>,
  },
  {
    header: "READY",
    width: "10%",
    accessor: (daemonSet: DaemonSet) => getReadinessComponent(daemonSet).element,
    title: (daemonSet: DaemonSet) => getReadinessComponent(daemonSet).title,
  },
  {
    header: "PODS",
    width: "10%",
    accessor: (daemonSet: DaemonSetWithResources) => (
      <>
        {daemonSet?.pods?.map((pod) => (
          <span
            title={pod.metadata.name}
            style={{
              "display": "inline-block",
              "width": "10px",
              "height": "10px",
              "border-radius": "5%",
              "background-color": getPodColor(pod.status.phase),
              "margin": "0 2px",
            } as any}
          >
          </span>
        ))}
      </>
    ),
  },
  {
    header: "UP-TO-DATE",
    width: "10%",
    accessor: (daemonSet: DaemonSet) => <>{daemonSet?.status?.updatedNumberScheduled || 0}</>,
  },
  {
    header: "AVAILABLE",
    width: "10%",
    accessor: (daemonSet: DaemonSet) => <>{daemonSet?.status?.numberAvailable || 0}</>,
  },
  {
    header: "AGE",
    width: "15%",
    accessor: (daemonSet: DaemonSet) => 
      useCalculateAge(daemonSet.metadata.creationTimestamp || "")(),
    sortable: true,
    sortFunction: (items: any[], ascending: boolean) => sortByAge(items, ascending),
  },
];

// Filter for DaemonSet based on readiness
export const daemonSetReadinessFilter: Filter = {
  name: "daemonSetReadiness",
  label: "Status",
  options: [
    { value: "ready", label: "Ready" },
    { value: "notReady", label: "Not Ready" },
  ],
  filterFunction: (daemonSet: DaemonSet, value: string) => {
    const desiredScheduled = daemonSet.status?.desiredNumberScheduled;
    const numberReady = daemonSet.status?.numberReady;
    const numberUnavailable = daemonSet.status?.numberUnavailable || 0;
    
    const isReady = numberReady === desiredScheduled && numberUnavailable === 0;
    
    return (value === "ready" && isReady) || (value === "notReady" && !isReady);
  },
}; 